﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MusicManager : Singleton<MusicManager> {

#region 获取基本字段
    [SerializeField, Tooltip("所有音乐音效的集合")]
    public AudioClip[] music;
    [Tooltip("音乐声音的大小")]
    private float musicValue;
    [Tooltip("是否暂停音乐")]
    private bool isNStopM;
    [Tooltip("音效声音的大小")]
    private float soundValue;
    [Tooltip("是否暂停音效")]
    private bool isNStopS;
    [Tooltip("获取所有音乐的Audio")]
    public List<MusicDir> musicDir =new List<MusicDir>();
    public ArrayList array = new ArrayList();
    #endregion
#region 声音的大小是否停止属性
    public float MusicValue
    {
        get { return musicValue; }
        set
        {
            musicValue = value;
            VoiceChange(MusicOrSound.Music,value);
        }
    }
    public float SoundValue
    {
        get { return soundValue; }
        set
        {
            soundValue = value;
            VoiceChange(MusicOrSound.Sound, value);
        }
    }
    public bool IsNStopM
    {
        get { return isNStopM; }
        set {
            isNStopM = value;
            VoiceChange(MusicOrSound.Music, value);
        }
    }
    public bool IsNStopS
    {
        get { return isNStopS; }
        set {
            isNStopS = value;
            VoiceChange(MusicOrSound.Sound, value);
        }
    }
    #endregion

#region 增加音乐播放器和减少音乐播放器
    public void AddMusic(MusicOrSound ms,AudioSource aus)
    {
        MusicDir md = new MusicDir(ms, aus);
        musicDir.Add(md);
    }
    public void RemoveMusic(AudioSource aus)
    {
        int i = 0;
        foreach (MusicDir x in musicDir)
        {
            if (x.Aus == aus)
            {
                musicDir.RemoveAt(i);
                break;
            }
            i++;
        }
    }
    #endregion

    public void VoiceChange(MusicOrSound ms, float v=0)
    {
        foreach(MusicDir md in musicDir)
        {
            if(md.MS==ms)
            {
                md.Aus.volume = v;
            }
        }
    }//改变大小
    public void VoiceChange(MusicOrSound ms,bool isStop)
    {
        foreach (MusicDir md in musicDir)
        {
            if (md.MS == ms)
            {
                if (!isStop) md.Aus.Stop();
                else
                {
                    md.Aus.Play();
                }
            }
        }
    }//改变是否播放

    public override void FixedAwake()
    {
        int i = 0;
        bool ss;
        array = TextClass.Instance.MFLoad(true, out ss);
        foreach (MusicInform mi in array)
        {
            if (i == 0)
            {
                musicValue = mi.Volume;
                isNStopM = mi.IsOpen;
            }
            else
            {
                soundValue = mi.Volume;
                isNStopS = mi.IsOpen;
            }
            i++;
        }
    }
}
public class MusicDir
{
    private MusicOrSound ms;
    private AudioSource audios;
    public MusicOrSound MS
    {
        get { return ms; }
        set { ms = value; }
    }
    public AudioSource Aus
    {
        get { return audios; }
        set { audios = value; }
    }
    public MusicDir(MusicOrSound mos,AudioSource aus)
    {
        ms = mos;
        audios = aus;
    }
}
public enum KOMusic
{
    NULL,BGM, SLaser,BOOM, Explosion_player,Player_InJured,Button,ButtonStart,
    OpenDoor,GameOver, Explosion_enemy, Enemy_InJured, Prop_PickUp,AddHeath
}//音乐种类的枚举
public enum MusicOrSound
{
    Music,Sound
}//声音种类的枚举
